#include <asio.hpp>
#include <iostream>

using asio::ip::udp;

int main(int argc, char* argv[]) {
    try {
        if (argc != 3) {
            std::cerr << "Usage: udp_client <host> <port>\n";
            return 1;
        }

        asio::io_context io_context;
        udp::resolver resolver(io_context);
        udp::endpoint endpoint = *resolver.resolve(udp::v4(), argv[1], argv[2]).begin();
        udp::socket socket(io_context);

        socket.open(udp::v4());

        const std::string message = "Hello from client!";
        socket.send_to(asio::buffer(message), endpoint);

        char reply[1024];
        udp::endpoint sender_endpoint;
        size_t reply_length = socket.receive_from(asio::buffer(reply, 1024), sender_endpoint);

        std::cout << "Reply from server: " << std::string(reply, reply_length) << std::endl;
    } catch (std::exception& e) {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}
